home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / DirectMusic / 3DAudio / 3DAudio.cpp next >
C/C++ Source or Header  |  2001-10-31  |  29KB  |  766 lines

  1. //-----------------------------------------------------------------------------
  2. // File: 3DAudio.cpp
  3. //
  4. // Desc: Plays a primary segment using DirectMusic 
  5. //
  6. // Copyright (c) 2000-2001 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #define STRICT
  9. #include <windows.h>
  10. #include <basetsd.h>
  11. #include <commdlg.h>
  12. #include <commctrl.h>
  13. #include <dmusicc.h>
  14. #include <dmusici.h>
  15. #include <dxerr8.h>
  16. #include <cguid.h>
  17. #include <math.h>
  18. #include <stdio.h>
  19. #include "resource.h"
  20. #include "DMUtil.h"
  21. #include "DXUtil.h"
  22.  
  23.  
  24.  
  25.  
  26. //-----------------------------------------------------------------------------
  27. // Function-prototypes
  28. //-----------------------------------------------------------------------------
  29. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
  30. VOID    OnInitDialog( HWND hDlg );
  31. HRESULT InitAudio( HWND hDlg );
  32. VOID    SetSlidersPos( HWND hDlg, FLOAT fDopplerValue, FLOAT fRolloffValue, FLOAT fMinDistValue, FLOAT fMaxDistValue );
  33. VOID    OnOpenAudioFile( HWND hDlg );
  34. HRESULT LoadSegmentFile( HWND hDlg, TCHAR* strFileName );
  35. HRESULT OnPlayAudio( HWND hDlg );
  36. VOID    EnablePlayUI( HWND hDlg, BOOL bEnable );
  37. VOID    OnMovementTimer( HWND hDlg );
  38. VOID    Set3DParameters( FLOAT fDopplerFactor, FLOAT fRolloffFactor, FLOAT fMinDistance,   FLOAT fMaxDistance );
  39. VOID    EnablePlayUI( HWND hDlg, BOOL bEnable );
  40. VOID    SetObjectProperties( D3DVECTOR* pvPosition, D3DVECTOR* pvVelocity );
  41. VOID    UpdateGrid( HWND hDlg, FLOAT x, FLOAT y );
  42. VOID    OnSliderChanged( HWND hDlg );
  43. VOID    SetObjectProperties( D3DVECTOR* pvPosition, D3DVECTOR* pvVelocity );
  44. FLOAT   ConvertLinearSliderPosToLogScale( LONG lSliderPos );
  45. LONG    ConvertLogScaleToLinearSliderPosTo( FLOAT fValue );
  46.  
  47.  
  48.  
  49.  
  50.  
  51. //-----------------------------------------------------------------------------
  52. // Defines, constants, and global variables
  53. //-----------------------------------------------------------------------------
  54. #define ORBIT_MAX_RADIUS        5.0f
  55. #define IDT_MOVEMENT_TIMER      1
  56.  
  57. CMusicManager*          g_pMusicManager          = NULL;
  58. CMusicSegment*          g_pMusicSegment          = NULL;
  59. IDirectMusicAudioPath*  g_p3DAudioPath           = NULL;
  60. IDirectSound3DBuffer*   g_pDS3DBuffer            = NULL;   // 3D sound buffer
  61. IDirectSound3DListener* g_pDSListener            = NULL;   // 3D listener object
  62. DS3DBUFFER              g_dsBufferParams;                  // 3D buffer properties
  63. DS3DLISTENER            g_dsListenerParams;                // Listener properties
  64. HINSTANCE               g_hInst                  = NULL;
  65. BOOL                    g_bAllowMovementTimer    = TRUE;
  66. BOOL                    g_bDeferSettings         = FALSE;
  67.  
  68.  
  69.  
  70.  
  71. //-----------------------------------------------------------------------------
  72. // Name: WinMain()
  73. // Desc: Entry point for the application.  Since we use a simple dialog for 
  74. //       user interaction we don't need to pump messages.
  75. //-----------------------------------------------------------------------------
  76. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, 
  77.                       INT nCmdShow )
  78. {
  79.     g_hInst = hInst;
  80.     
  81.     InitCommonControls();
  82.  
  83.     // Display the main dialog box.
  84.     DialogBox( hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc );
  85.     
  86.     return TRUE;
  87. }
  88.  
  89.  
  90.  
  91.  
  92. //-----------------------------------------------------------------------------
  93. // Name: MainDlgProc()
  94. // Desc: Handles dialog messages
  95. //-----------------------------------------------------------------------------
  96. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  97. {
  98.     HRESULT hr;
  99.  
  100.     switch( msg ) 
  101.     {
  102.         case WM_INITDIALOG:
  103.             OnInitDialog( hDlg );
  104.             break;
  105.  
  106.         case WM_COMMAND:
  107.             switch( LOWORD(wParam) )
  108.             {
  109.                 case IDC_SOUNDFILE:
  110.                     OnOpenAudioFile( hDlg );
  111.                     break;
  112.  
  113.                 case IDCANCEL:
  114.                     EndDialog( hDlg, 0 );
  115.                     break;
  116.  
  117.                 case IDC_PLAY:
  118.                     if( FAILED( hr = OnPlayAudio( hDlg ) ) )
  119.                     {
  120.                         DXTRACE_ERR( TEXT("OnPlayAudio"), hr );
  121.                         MessageBox( hDlg, "Error playing DirectMusic segment. "
  122.                                     "Sample will now exit.", "DirectMusic Sample", 
  123.                                     MB_OK | MB_ICONERROR );
  124.                         EndDialog( hDlg, 0 );
  125.                     }
  126.                     break;
  127.  
  128.                 case IDC_STOP:
  129.                     if( g_pMusicSegment )
  130.                     {
  131.                         g_pMusicSegment->Stop( DMUS_SEGF_BEAT ); 
  132.                         EnablePlayUI( hDlg, TRUE );
  133.                     }
  134.                     break;
  135.  
  136.                 case IDC_DEFER:
  137.                     g_bDeferSettings = !g_bDeferSettings;
  138.                     OnSliderChanged( hDlg );                    
  139.                     break;
  140.  
  141.                 case IDC_APPLY:
  142.                     // Call the IDirectSound3DListener::CommitDeferredSettings 
  143.                     // method to execute all of the deferred commands at once.
  144.                     // This is many times more efficent than recomputing everything
  145.                     // for every call.
  146.                     if( g_pDSListener )
  147.                         g_pDSListener->CommitDeferredSettings();
  148.                     break;
  149.  
  150.                 default:
  151.                     return FALSE; // Didn't handle message
  152.             }
  153.             break;
  154.  
  155.         case WM_TIMER:
  156.             if( wParam == IDT_MOVEMENT_TIMER )
  157.                 OnMovementTimer( hDlg );
  158.             break;
  159.  
  160.         case WM_NOTIFY:
  161.             OnSliderChanged( hDlg );
  162.             break;
  163.  
  164.         case WM_DESTROY:
  165.             // Cleanup everything
  166.             if( g_pMusicSegment )
  167.             {
  168.                 g_pMusicSegment->Unload( g_p3DAudioPath );
  169.                 SAFE_DELETE( g_pMusicSegment );
  170.             }
  171.  
  172.             KillTimer( hDlg, 1 );    
  173.             SAFE_RELEASE( g_pDSListener );
  174.             SAFE_RELEASE( g_pDS3DBuffer );
  175.             SAFE_RELEASE( g_p3DAudioPath );
  176.  
  177.             SAFE_DELETE( g_pMusicManager );
  178.             break; 
  179.  
  180.         default:
  181.             return FALSE; // Didn't handle message
  182.     }
  183.  
  184.     return TRUE; // Handled message
  185. }
  186.  
  187.  
  188.  
  189.  
  190. //-----------------------------------------------------------------------------
  191. // Name: OnInitDialog()
  192. // Desc: Initializes the dialogs (sets up UI controls, etc.)
  193. //-----------------------------------------------------------------------------
  194. VOID OnInitDialog( HWND hDlg )
  195. {
  196.     HRESULT hr;
  197.  
  198.     // Load the icon
  199.     HICON hIcon = LoadIcon( g_hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
  200.  
  201.     // Set the icon for this dialog.
  202.     SendMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM) hIcon );  // Set big icon
  203.     SendMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon );  // Set small icon
  204.  
  205.     if( FAILED( hr = InitAudio( hDlg ) ) )
  206.     {
  207.         DXTRACE_ERR( TEXT("InitAudio"), hr );
  208.         MessageBox( hDlg, "Error initializing DirectMusic.  Sample will now exit.", 
  209.                           "DirectMusic Sample", MB_OK | MB_ICONERROR );
  210.         EndDialog( hDlg, 0 );
  211.         return;
  212.     }
  213.  
  214.     // Set the default media path (something like C:\MSSDK\SAMPLES\MULTIMEDIA\MEDIA)
  215.     // to be used as the search directory for finding DirectMusic content.
  216.     g_pMusicManager->SetSearchDirectory( DXUtil_GetDXSDKMediaPath() );
  217.  
  218.     // Load a default music segment 
  219.     TCHAR strFileName[MAX_PATH];
  220.     strcpy( strFileName, DXUtil_GetDXSDKMediaPath() );
  221.     strcat( strFileName, "sample.sgt" );
  222.     if( S_FALSE == LoadSegmentFile( hDlg, strFileName ) )
  223.     {
  224.         // Set the UI controls
  225.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("No file loaded.") );
  226.     }
  227.  
  228.     // Create a timer to periodically move the 3D object around
  229.     SetTimer( hDlg, IDT_MOVEMENT_TIMER, 5, NULL );
  230.  
  231.     // Get handles to dialog items
  232.     HWND hDopplerSlider  = GetDlgItem( hDlg, IDC_DOPPLER_SLIDER );
  233.     HWND hRolloffSlider  = GetDlgItem( hDlg, IDC_ROLLOFF_SLIDER );
  234.     HWND hMinDistSlider  = GetDlgItem( hDlg, IDC_MINDISTANCE_SLIDER );
  235.     HWND hMaxDistSlider  = GetDlgItem( hDlg, IDC_MAXDISTANCE_SLIDER );
  236.     HWND hVertSlider     = GetDlgItem( hDlg, IDC_VERTICAL_SLIDER );
  237.     HWND hHorzSlider     = GetDlgItem( hDlg, IDC_HORIZONTAL_SLIDER );
  238.  
  239.     // Set the range and position of the sliders
  240.     PostMessage( hDopplerSlider, TBM_SETRANGEMAX, TRUE, 40L );
  241.     PostMessage( hDopplerSlider, TBM_SETRANGEMIN, TRUE, 0L );
  242.  
  243.     PostMessage( hRolloffSlider, TBM_SETRANGEMAX, TRUE, 40L );
  244.     PostMessage( hRolloffSlider, TBM_SETRANGEMIN, TRUE, 0L );
  245.  
  246.     PostMessage( hMinDistSlider, TBM_SETRANGEMAX, TRUE, 40L );
  247.     PostMessage( hMinDistSlider, TBM_SETRANGEMIN, TRUE, 1L );
  248.  
  249.     PostMessage( hMaxDistSlider, TBM_SETRANGEMAX, TRUE, 40L );
  250.     PostMessage( hMaxDistSlider, TBM_SETRANGEMIN, TRUE, 1L );
  251.  
  252.     PostMessage( hVertSlider,    TBM_SETRANGEMAX, TRUE, 100L );
  253.     PostMessage( hVertSlider,    TBM_SETRANGEMIN, TRUE, -100L );
  254.     PostMessage( hVertSlider,    TBM_SETPOS,      TRUE, 10L );
  255.  
  256.     PostMessage( hHorzSlider,    TBM_SETRANGEMAX, TRUE, 100L );
  257.     PostMessage( hHorzSlider,    TBM_SETRANGEMIN, TRUE, -100L );
  258.     PostMessage( hHorzSlider,    TBM_SETPOS,      TRUE, 50L );
  259.  
  260.     // Set the position of the sliders
  261.     SetSlidersPos( hDlg, 0.0f, 0.0f, ORBIT_MAX_RADIUS, ORBIT_MAX_RADIUS*2.0f );
  262. }
  263.  
  264.  
  265.  
  266.  
  267. //-----------------------------------------------------------------------------
  268. // Name: InitAudio()
  269. // Desc: Init both DirectMusic and DirectSound
  270. //-----------------------------------------------------------------------------
  271. HRESULT InitAudio( HWND hDlg )
  272. {
  273.     HRESULT hr;
  274.  
  275.     // Initialize the performance. This initializes both DirectMusic and DirectSound
  276.     // and optionally sets up the synthesizer and default audio path.
  277.     // However, since this app never uses the default audio path, we don't bother 
  278.     // to do that here.
  279.     g_pMusicManager = new CMusicManager();
  280.     if( FAILED( hr = g_pMusicManager->Initialize( hDlg ) ) )
  281.         return DXTRACE_ERR( TEXT("Initialize"), hr );
  282.  
  283.     IDirectMusicPerformance8* pPerformance = g_pMusicManager->GetPerformance();
  284.  
  285.     // Create a 3D audiopath with a 3d buffer.
  286.     // We can then play all segments into this buffer and directly control its
  287.     // 3D parameters.
  288.     if( FAILED( hr = pPerformance->CreateStandardAudioPath( DMUS_APATH_DYNAMIC_3D, 
  289.                                                             64, TRUE, &g_p3DAudioPath ) ) )
  290.         return DXTRACE_ERR( TEXT("CreateStandardAudioPath"), hr );
  291.  
  292.     // Get the 3D buffer in the audio path.
  293.     if( FAILED( hr = g_p3DAudioPath->GetObjectInPath( 0, DMUS_PATH_BUFFER, 0,
  294.                                                       GUID_NULL, 0, IID_IDirectSound3DBuffer, 
  295.                                                       (LPVOID*) &g_pDS3DBuffer ) ) )
  296.         return DXTRACE_ERR( TEXT("GetObjectInPath"), hr );
  297.  
  298.     // Get the 3D buffer parameters
  299.     g_dsBufferParams.dwSize = sizeof(DS3DBUFFER);
  300.     g_pDS3DBuffer->GetAllParameters( &g_dsBufferParams );
  301.  
  302.     // Set new 3D buffer parameters
  303.     g_dsBufferParams.dwMode = DS3DMODE_HEADRELATIVE;
  304.     g_pDS3DBuffer->SetAllParameters( &g_dsBufferParams, DS3D_IMMEDIATE );
  305.     
  306.     // Get the listener from the in the audio path.
  307.     if( FAILED( hr = g_p3DAudioPath->GetObjectInPath( 0, DMUS_PATH_PRIMARY_BUFFER, 0,
  308.                                                       GUID_NULL, 0, IID_IDirectSound3DListener, 
  309.                                                       (LPVOID*) &g_pDSListener ) ) )
  310.         return DXTRACE_ERR( TEXT("GetObjectInPath"), hr );
  311.  
  312.     // Get listener parameters
  313.     g_dsListenerParams.dwSize = sizeof(DS3DLISTENER);
  314.     g_pDSListener->GetAllParameters( &g_dsListenerParams );
  315.  
  316.     return S_OK;
  317. }
  318.  
  319.  
  320.  
  321.  
  322. //-----------------------------------------------------------------------------
  323. // Name: SetSlidersPos()
  324. // Desc: Sets the slider positions
  325. //-----------------------------------------------------------------------------
  326. VOID SetSlidersPos( HWND hDlg, FLOAT fDopplerValue, FLOAT fRolloffValue,
  327.                     FLOAT fMinDistValue, FLOAT fMaxDistValue )
  328. {
  329.     HWND hDopplerSlider  = GetDlgItem( hDlg, IDC_DOPPLER_SLIDER );
  330.     HWND hRolloffSlider  = GetDlgItem( hDlg, IDC_ROLLOFF_SLIDER );
  331.     HWND hMinDistSlider  = GetDlgItem( hDlg, IDC_MINDISTANCE_SLIDER );
  332.     HWND hMaxDistSlider  = GetDlgItem( hDlg, IDC_MAXDISTANCE_SLIDER );
  333.  
  334.     LONG lDopplerSlider = ConvertLogScaleToLinearSliderPosTo( fDopplerValue );
  335.     LONG lRolloffSlider = ConvertLogScaleToLinearSliderPosTo( fRolloffValue );
  336.     LONG lMinDistSlider = ConvertLogScaleToLinearSliderPosTo( fMinDistValue );
  337.     LONG lMaxDistSlider = ConvertLogScaleToLinearSliderPosTo( fMaxDistValue );
  338.  
  339.     SendMessage( hDopplerSlider, TBM_SETPOS, TRUE, lDopplerSlider );
  340.     SendMessage( hRolloffSlider, TBM_SETPOS, TRUE, lRolloffSlider );
  341.     SendMessage( hMinDistSlider, TBM_SETPOS, TRUE, lMinDistSlider );
  342.     SendMessage( hMaxDistSlider, TBM_SETPOS, TRUE, lMaxDistSlider );
  343. }
  344.  
  345.  
  346.  
  347.  
  348. //-----------------------------------------------------------------------------
  349. // Name: OnOpenAudioFile()
  350. // Desc: Called when the user requests to open a sound file
  351. //-----------------------------------------------------------------------------
  352. VOID OnOpenAudioFile( HWND hDlg ) 
  353. {
  354.     static TCHAR strFileName[MAX_PATH] = TEXT("");
  355.     static TCHAR strPath[MAX_PATH] = TEXT("");
  356.  
  357.     // Get the default media path (something like C:\MSSDK\SAMPLES\DMUSIC\MEDIA)
  358.     if( '\0' == strPath[0] )
  359.     {
  360.         const TCHAR* szDir = DXUtil_GetDXSDKMediaPath();
  361.         strcpy( strPath, szDir );
  362.     }
  363.  
  364.     // Setup the OPENFILENAME structure
  365.     OPENFILENAME ofn = { sizeof(OPENFILENAME), hDlg, NULL,
  366.                          TEXT("DirectMusic Content Files\0*.sgt;*.mid;*.rmi\0Wave Files\0*.wav\0All Files\0*.*\0\0"), NULL,
  367.                          0, 1, strFileName, MAX_PATH, NULL, 0, strPath,
  368.                          TEXT("Open Content File"),
  369.                          OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, 0, 0,
  370.                          TEXT(".sgt"), 0, NULL, NULL };
  371.  
  372.     if( g_pMusicSegment )
  373.         g_pMusicSegment->Stop( 0 );
  374.  
  375.   // Update the UI controls to show the sound as loading a file
  376.     EnableWindow( GetDlgItem( hDlg, IDC_PLAY ), FALSE );
  377.     EnableWindow( GetDlgItem( hDlg, IDC_STOP ), FALSE );
  378.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Loading file...") );
  379.  
  380.     // Stop the timer while dialogs are displayed
  381.     g_bAllowMovementTimer = FALSE;
  382.  
  383.     // Display the OpenFileName dialog. Then, try to load the specified file
  384.     if( TRUE != GetOpenFileName( &ofn ) )
  385.     {
  386.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Load aborted.") );
  387.         g_bAllowMovementTimer = TRUE;
  388.         return;
  389.     }
  390.  
  391.     if( S_FALSE == LoadSegmentFile( hDlg, strFileName ) )
  392.     {
  393.         // Not a critical failure, so just update the status
  394.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Could not create segment from file.") );
  395.     }
  396.  
  397.     g_bAllowMovementTimer = TRUE;
  398.  
  399.     // Remember the path for next time
  400.     strcpy( strPath, strFileName );
  401.     char* strLastSlash = strrchr( strPath, '\\' );
  402.     strLastSlash[0] = '\0';
  403. }
  404.  
  405.  
  406.  
  407.  
  408. //-----------------------------------------------------------------------------
  409. // Name: LoadSegmentFile()
  410. // Desc: 
  411. //-----------------------------------------------------------------------------
  412. HRESULT LoadSegmentFile( HWND hDlg, TCHAR* strFileName )
  413. {
  414.     HRESULT hr;
  415.      
  416.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  417.  
  418.     // Free any previous segment, and make a new one
  419.     SAFE_DELETE( g_pMusicSegment );
  420.  
  421.     // Have the loader collect any garbage now that the old 
  422.     // segment has been released
  423.     g_pMusicManager->CollectGarbage();
  424.  
  425.     // For DirectMusic must know if the file is a standard MIDI file or not
  426.     // in order to load the correct instruments.
  427.     BOOL bMidiFile = FALSE;
  428.     if( strstr( strFileName, ".mid" ) != NULL ||
  429.         strstr( strFileName, ".rmi" ) != NULL ) 
  430.     {
  431.         bMidiFile = TRUE;
  432.     }
  433.  
  434.     // Load the file into a DirectMusic segment, but don't download 
  435.     // it to the default audio path -- use the 3D audio path instead
  436.     if( FAILED( g_pMusicManager->CreateSegmentFromFile( &g_pMusicSegment, strFileName, 
  437.                                                         FALSE, bMidiFile ) ) )
  438.     {
  439.         // Not a critical failure, so just update the status
  440.         return S_FALSE; 
  441.     }
  442.  
  443.     // Download the segment on the 3D audio path
  444.     if( FAILED( hr = g_pMusicSegment->Download( g_p3DAudioPath ) ) )
  445.         return DXTRACE_ERR( TEXT("Download"), hr );
  446.  
  447.     // Update the UI controls to show the segment is loaded
  448.     SetDlgItemText( hDlg, IDC_FILENAME, strFileName );
  449.     EnablePlayUI( hDlg, TRUE );
  450.  
  451.     return S_OK;
  452. }
  453.  
  454.  
  455.  
  456.  
  457. //-----------------------------------------------------------------------------
  458. // Name: OnPlayAudio()
  459. // Desc: 
  460. //-----------------------------------------------------------------------------
  461. HRESULT OnPlayAudio( HWND hDlg )
  462. {
  463.     HRESULT hr;
  464.  
  465.     if( g_pMusicSegment == NULL )
  466.         return S_FALSE;
  467.  
  468.     // Set the segment to repeat many times
  469.     if( FAILED( hr = g_pMusicSegment->SetRepeats( DMUS_SEG_REPEAT_INFINITE ) ) )
  470.         return DXTRACE_ERR( TEXT("SetRepeats"), hr );
  471.  
  472.     // Play the segment and wait. The DMUS_SEGF_BEAT indicates to play on the 
  473.     // next beat if there is a segment currently playing. 
  474.     if( FAILED( hr = g_pMusicSegment->Play( DMUS_SEGF_BEAT, g_p3DAudioPath ) ) )
  475.         return DXTRACE_ERR( TEXT("Play"), hr );
  476.  
  477.     EnablePlayUI( hDlg, FALSE );
  478.  
  479.     return S_OK;
  480. }
  481.  
  482.  
  483.  
  484.  
  485. //-----------------------------------------------------------------------------
  486. // Name: EnablePlayUI( hDlg,)
  487. // Desc: Enables or disables the Play UI controls 
  488. //-----------------------------------------------------------------------------
  489. VOID EnablePlayUI( HWND hDlg, BOOL bEnable )
  490. {
  491.     if( bEnable )
  492.     {
  493.         EnableWindow(   GetDlgItem( hDlg, IDC_STOP ),       FALSE );
  494.  
  495.         EnableWindow(   GetDlgItem( hDlg, IDC_PLAY ),       TRUE );
  496.         SetFocus(       GetDlgItem( hDlg, IDC_PLAY ) );
  497.     }
  498.     else
  499.     {
  500.         EnableWindow(  GetDlgItem( hDlg, IDC_STOP ),       TRUE );
  501.         SetFocus(      GetDlgItem( hDlg, IDC_STOP ) );
  502.         EnableWindow(  GetDlgItem( hDlg, IDC_PLAY ),       FALSE );
  503.     }
  504. }
  505.  
  506.  
  507.  
  508.  
  509. //-----------------------------------------------------------------------------
  510. // Name: OnSliderChanged()  
  511. // Desc: Called when the dialog's slider bars are changed by the user, or need
  512. //       updating
  513. //-----------------------------------------------------------------------------
  514. VOID OnSliderChanged( HWND hDlg )
  515. {
  516.     TCHAR strBuffer[10];
  517.     FLOAT fDopplerFactor;
  518.     FLOAT fRolloffFactor;
  519.     FLOAT fMinDistance; 
  520.     FLOAT fMaxDistance;
  521.  
  522.     // Get handles to dialog items
  523.     HWND hDopplerSlider  = GetDlgItem( hDlg, IDC_DOPPLER_SLIDER );
  524.     HWND hRolloffSlider  = GetDlgItem( hDlg, IDC_ROLLOFF_SLIDER );
  525.     HWND hMinDistSlider  = GetDlgItem( hDlg, IDC_MINDISTANCE_SLIDER );
  526.     HWND hMaxDistSlider  = GetDlgItem( hDlg, IDC_MAXDISTANCE_SLIDER );
  527.  
  528.     // Get the position of the sliders
  529.     fDopplerFactor = ConvertLinearSliderPosToLogScale( (long)SendMessage( hDopplerSlider, TBM_GETPOS, 0, 0 ) );
  530.     fRolloffFactor = ConvertLinearSliderPosToLogScale( (long)SendMessage( hRolloffSlider, TBM_GETPOS, 0, 0 ) );
  531.     fMinDistance   = ConvertLinearSliderPosToLogScale( (long)SendMessage( hMinDistSlider, TBM_GETPOS, 0, 0 ) );
  532.     fMaxDistance   = ConvertLinearSliderPosToLogScale( (long)SendMessage( hMaxDistSlider, TBM_GETPOS, 0, 0 ) );
  533.  
  534.     // Set the static text boxes
  535.     sprintf( strBuffer, TEXT("%.2f"), fDopplerFactor );
  536.     SetWindowText( GetDlgItem( hDlg, IDC_DOPPLERFACTOR ), strBuffer );
  537.  
  538.     sprintf( strBuffer, TEXT("%.2f"), fRolloffFactor );
  539.     SetWindowText( GetDlgItem( hDlg, IDC_ROLLOFFFACTOR ), strBuffer );
  540.  
  541.     sprintf( strBuffer, TEXT("%.2f"), fMinDistance );
  542.     SetWindowText( GetDlgItem( hDlg, IDC_MINDISTANCE ), strBuffer );
  543.  
  544.     sprintf( strBuffer, TEXT("%.2f"), fMaxDistance );
  545.     SetWindowText( GetDlgItem( hDlg, IDC_MAXDISTANCE ), strBuffer );
  546.  
  547.     // Set the options in the DirectSound buffer
  548.     Set3DParameters( fDopplerFactor, fRolloffFactor, fMinDistance, fMaxDistance );
  549.  
  550.     EnableWindow( GetDlgItem( hDlg, IDC_APPLY ), g_bDeferSettings );
  551. }
  552.  
  553.  
  554.  
  555.  
  556. //-----------------------------------------------------------------------------
  557. // Name: Set3DParameters()
  558. // Desc: Set the 3D buffer parameters
  559. //-----------------------------------------------------------------------------
  560. VOID Set3DParameters( FLOAT fDopplerFactor, FLOAT fRolloffFactor,
  561.                       FLOAT fMinDistance,   FLOAT fMaxDistance )
  562. {
  563.     // Every change to 3-D sound buffer and listener settings causes 
  564.     // DirectSound to remix, at the expense of CPU cycles. 
  565.     // To minimize the performance impact of changing 3-D settings, 
  566.     // use the DS3D_DEFERRED flag in the dwApply parameter of any of 
  567.     // the IDirectSound3DListener or IDirectSound3DBuffer methods that 
  568.     // change 3-D settings. Then call the IDirectSound3DListener::CommitDeferredSettings 
  569.     // method to execute all of the deferred commands at once.
  570.     DWORD dwApplyFlag = ( g_bDeferSettings ) ? DS3D_DEFERRED : DS3D_IMMEDIATE;
  571.  
  572.     g_dsListenerParams.flDopplerFactor = fDopplerFactor;
  573.     g_dsListenerParams.flRolloffFactor = fRolloffFactor;
  574.  
  575.     if( g_pDSListener )
  576.         g_pDSListener->SetAllParameters( &g_dsListenerParams, dwApplyFlag );
  577.  
  578.     g_dsBufferParams.flMinDistance = fMinDistance;
  579.     g_dsBufferParams.flMaxDistance = fMaxDistance;
  580.  
  581.     if( g_pDS3DBuffer )
  582.         g_pDS3DBuffer->SetAllParameters( &g_dsBufferParams, dwApplyFlag );
  583. }
  584.  
  585.  
  586.  
  587.  
  588. //-----------------------------------------------------------------------------
  589. // Name: OnMovementTimer()
  590. // Desc: Periodically updates the position of the object 
  591. //-----------------------------------------------------------------------------
  592. VOID OnMovementTimer( HWND hDlg ) 
  593. {
  594.     FLOAT fXScale;
  595.     FLOAT fYScale;
  596.  
  597.     if( !g_bAllowMovementTimer )
  598.         return;
  599.  
  600.     HWND hHorzSlider = GetDlgItem( hDlg, IDC_HORIZONTAL_SLIDER );
  601.     HWND hVertSlider = GetDlgItem( hDlg, IDC_VERTICAL_SLIDER );
  602.  
  603.     fXScale = SendMessage( hHorzSlider, TBM_GETPOS, 0, 0 ) / 100.0f;
  604.     fYScale = SendMessage( hVertSlider, TBM_GETPOS, 0, 0 ) / 100.0f;
  605.     FLOAT t = timeGetTime()/1000.0f;
  606.  
  607.     // Move the sound object around the listener. The maximum radius of the
  608.     // orbit is 27.5 units.
  609.     D3DVECTOR vPosition;
  610.     vPosition.x = ORBIT_MAX_RADIUS * fXScale * (FLOAT)sin(t);
  611.     vPosition.y = 0.0f;
  612.     vPosition.z = ORBIT_MAX_RADIUS * fYScale * (FLOAT)cos(t);
  613.  
  614.     D3DVECTOR vVelocity;
  615.     vVelocity.x = ORBIT_MAX_RADIUS * fXScale * (FLOAT)sin(t+0.05f);
  616.     vVelocity.y = 0.0f;
  617.     vVelocity.z = ORBIT_MAX_RADIUS * fYScale * (FLOAT)cos(t+0.05f);
  618.  
  619.     // Show the object's position on the dialog's grid control
  620.     UpdateGrid( hDlg, vPosition.x, vPosition.z );
  621.  
  622.     // Set the sound buffer velocity and position
  623.     SetObjectProperties( &vPosition, &vVelocity );
  624. }
  625.  
  626.  
  627.  
  628.  
  629. //-----------------------------------------------------------------------------
  630. // Name: UpdateGrid()
  631. // Desc: Draws a red dot in the dialog's grid bitmap at the x,y coordinate.
  632. //-----------------------------------------------------------------------------
  633. VOID UpdateGrid( HWND hDlg, FLOAT x, FLOAT y )
  634. {
  635.     static LONG s_lPixel[5] = { 0,0,0,0,0 };
  636.     static LONG s_lX = 0;
  637.     static LONG s_lY = 0;
  638.  
  639.     HWND hWndGrid = GetDlgItem( hDlg, IDC_RENDER_WINDOW );
  640.     HDC  hDC  = GetDC( hWndGrid );
  641.     RECT rc;
  642.     
  643.     // Replace pixels from that were overdrawn last time
  644.     SetPixel( hDC, s_lX-1, s_lY+0, s_lPixel[0] );
  645.     SetPixel( hDC, s_lX+0, s_lY-1, s_lPixel[1] );
  646.     SetPixel( hDC, s_lX+0, s_lY+0, s_lPixel[2] );
  647.     SetPixel( hDC, s_lX+0, s_lY+1, s_lPixel[3] );
  648.     SetPixel( hDC, s_lX+1, s_lY+0, s_lPixel[4] );
  649.  
  650.     // Convert the world space x,y coordinates to pixel coordinates
  651.     GetClientRect( hWndGrid, &rc );
  652.     s_lX = (LONG)( ( x/ORBIT_MAX_RADIUS + 1 ) * ( rc.left + rc.right ) / 2 );
  653.     s_lY = (LONG)( (-y/ORBIT_MAX_RADIUS + 1 ) * ( rc.top + rc.bottom ) / 2 );
  654.  
  655.     // Save the pixels before drawing the cross hair
  656.     s_lPixel[0] = GetPixel( hDC, s_lX-1, s_lY+0 );
  657.     s_lPixel[1] = GetPixel( hDC, s_lX+0, s_lY-1 );
  658.     s_lPixel[2] = GetPixel( hDC, s_lX+0, s_lY+0 );
  659.     s_lPixel[3] = GetPixel( hDC, s_lX+0, s_lY+1 );
  660.     s_lPixel[4] = GetPixel( hDC, s_lX+1, s_lY+0 );
  661.  
  662.     // Draw a crosshair object in red pixels
  663.     SetPixel( hDC, s_lX-1, s_lY+0, 0x000000ff );
  664.     SetPixel( hDC, s_lX+0, s_lY-1, 0x000000ff );
  665.     SetPixel( hDC, s_lX+0, s_lY+0, 0x000000ff );
  666.     SetPixel( hDC, s_lX+0, s_lY+1, 0x000000ff );
  667.     SetPixel( hDC, s_lX+1, s_lY+0, 0x000000ff );
  668.  
  669.     ReleaseDC( hWndGrid, hDC );
  670. }
  671.  
  672.  
  673.  
  674.  
  675. //-----------------------------------------------------------------------------
  676. // Name: SetObjectProperties()
  677. // Desc: Sets the position and velocity on the 3D buffer
  678. //-----------------------------------------------------------------------------
  679. VOID SetObjectProperties( D3DVECTOR* pvPosition, D3DVECTOR* pvVelocity )
  680. {
  681.     // Every change to 3-D sound buffer and listener settings causes 
  682.     // DirectSound to remix, at the expense of CPU cycles. 
  683.     // To minimize the performance impact of changing 3-D settings, 
  684.     // use the DS3D_DEFERRED flag in the dwApply parameter of any of 
  685.     // the IDirectSound3DListener or IDirectSound3DBuffer methods that 
  686.     // change 3-D settings. Then call the IDirectSound3DListener::CommitDeferredSettings 
  687.     // method to execute all of the deferred commands at once.
  688.     memcpy( &g_dsBufferParams.vPosition, pvPosition, sizeof(D3DVECTOR) );
  689.     memcpy( &g_dsBufferParams.vVelocity, pvVelocity, sizeof(D3DVECTOR) );
  690.  
  691.     if( g_pDS3DBuffer )
  692.     {
  693.         g_pDS3DBuffer->SetPosition( g_dsBufferParams.vPosition.x,
  694.                                     g_dsBufferParams.vPosition.y,
  695.                                     g_dsBufferParams.vPosition.z, DS3D_IMMEDIATE );
  696.  
  697.         g_pDS3DBuffer->SetVelocity( g_dsBufferParams.vVelocity.x,
  698.                                     g_dsBufferParams.vVelocity.y,
  699.                                     g_dsBufferParams.vVelocity.z, DS3D_IMMEDIATE );
  700.     }
  701. }
  702.  
  703.  
  704.  
  705.  
  706. //-----------------------------------------------------------------------------
  707. // Name: ConvertLinearSliderPosToLogScale()
  708. // Desc: Converts a linear slider position to a quasi logrithmic scale
  709. //-----------------------------------------------------------------------------
  710. FLOAT ConvertLinearSliderPosToLogScale( LONG lSliderPos )
  711. {
  712.     if( lSliderPos > 0 && lSliderPos <= 10 )
  713.     {
  714.         return lSliderPos*0.01f;
  715.     }
  716.     else if( lSliderPos > 10 && lSliderPos <= 20 )
  717.     {
  718.         return (lSliderPos-10)*0.1f;
  719.     }
  720.     else if( lSliderPos > 20 && lSliderPos <= 30 )
  721.     {
  722.         return (lSliderPos-20)*1.0f;
  723.     }
  724.     else if( lSliderPos > 30 && lSliderPos <= 40 )
  725.     {
  726.         return (lSliderPos-30)*10.0f;
  727.     }
  728.  
  729.     return 0.0f;
  730. }
  731.  
  732.  
  733.  
  734.  
  735. //-----------------------------------------------------------------------------
  736. // Name: ConvertLinearSliderPosToLogScale()
  737. // Desc: Converts a quasi logrithmic scale to a slider position
  738. //-----------------------------------------------------------------------------
  739. LONG ConvertLogScaleToLinearSliderPosTo( FLOAT fValue )
  740. {
  741.     if( fValue > 0.0f && fValue <= 0.1f )
  742.     {
  743.         return (LONG)(fValue/0.01f);
  744.     }
  745.     else if( fValue > 0.1f && fValue <= 1.0f )
  746.     {
  747.         return (LONG)(fValue/0.1f) + 10;
  748.     }
  749.     else if( fValue > 1.0f && fValue <= 10.0f )
  750.     {
  751.         return (LONG)(fValue/1.0f) + 20;
  752.     }
  753.     else if( fValue > 10.0f && fValue <= 100.0f )
  754.     {
  755.         return (LONG)(fValue/10.0f) + 30;
  756.     }
  757.  
  758.     return 0;
  759. }
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.